﻿using System;
using System.Collections.Generic;

using Caliburn.Micro;

namespace Cooking.Client.Specs.Support
{
    public class CoroutineEnumerator
    {
        private readonly IEnumerator<IResult> enumerator;

        public CoroutineEnumerator(IEnumerable<IResult> enumerator)
        {
            this.enumerator = enumerator.GetEnumerator();
        }

        public TCoroutine Next<TCoroutine>()
        {
            while (enumerator.MoveNext())
            {
                if (enumerator.Current is TCoroutine)
                {
                    return (TCoroutine)enumerator.Current;
                }
            }

            throw new InvalidOperationException("List of coroutines does not include " + typeof (TCoroutine).Name);
        }

        public void Finish()
        {
            while (enumerator.MoveNext()) 
            {}
        }
    }
}